package top.linuyx.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;
import top.linuyx.pojo.param.ShippingAddressInsertParam;
import top.linuyx.pojo.param.ShippingAddressUpdateParam;
import top.linuyx.pojo.vo.ShippingAddressVO;

import java.util.List;

/**
 * @Author Linuyx
 * @Description ShippingAddressMapper
 * @Date Created in 2021-04-20 15:07
 */

@Repository
public interface ShippingAddressMapper {

    /**
     * 根据用户id查询所有收货地址
     *
     * @return
     */
    List<ShippingAddressVO> listAllShippingAddressById(@Param("userId") Long userId);

    /**
     * 添加收货地址
     *
     * @param insertParam ShippingAddress添加请求体
     */
    void insertAddress(ShippingAddressInsertParam insertParam);

    /**
     * 根据id修改收货地址
     *
     * @param updateParam
     */
    void updateAddressById(ShippingAddressUpdateParam updateParam);

    /**
     * 根据id删除收货地址
     *
     * @param shippingAddressId
     */
    @Update("update shipping_address set is_deleted=1 where id=#{shippingAddressId}")
    void deleteAddressById(@Param("shippingAddressId") Long shippingAddressId);

    /**
     * 根据用户id查询默认收货地址id
     *
     * @param userId 用户id
     * @return 默认收货地址id
     */
    @Select("select id from shipping_address where user_id=#{userId} and is_default=1 and is_deleted=0")
    Long getDefaultShippingAddressByUserId(@Param("userId") Long userId);

    /**
     * 根据默认收货地址id取消默认收货地址
     *
     * @param shippingAddressId 默认收货地址id
     */
    @Update("update shipping_address set is_default=0 where id=#{shippingAddressId} and is_deleted=0")
    void updateDefaultShippingAddressByUserId(@Param("shippingAddressId") Long shippingAddressId);

    /**
     * 根据收货地址id查询用户id
     *
     * @param shippingAddressId 收货地址id
     * @return 用户id
     */
    @Select("select user_id from shipping_address where id=#{shippingAddressId} and is_deleted=0")
    Long getUserIdByShippingAddressId(@Param("shippingAddressId") Long shippingAddressId);

}
